package info.peigen.hotpot.common.core.result.manage;

import info.peigen.hotpot.common.core.result.PageResult;
import info.peigen.hotpot.common.core.result.ResultBase;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.core.result.ResultStatus;
import info.peigen.hotpot.common.core.result.facade.DtoAble;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * web工程返回实体
 *
 * @author czj
 */
@SuppressWarnings({"rawtypes", "unchecked"})
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@Accessors(chain = true)
public class WebResult<T> extends ResultBase implements DtoAble {
    @ApiModelProperty(value = "当前页数", position = 1)
    int page;
    @ApiModelProperty(value = "每页多少条", position = 2)
    int pageSize;
    @ApiModelProperty(value = "总条数", position = 3)
    int totalRow;
    @ApiModelProperty(value = "总页数", position = 4)
    int totalPage;
    @ApiModelProperty(value = "返回对象", position = 1)
    T   info;

    public WebResult() {
        setSuccess();
    }

    public static WebResult operate(boolean b) {
        if (b) {
            return WebResult.setSuccessResult();
        }
        return WebResult.setFailure();
    }

    public static WebResult setFailure(ResultCode resultCode) {
        WebResult webResult = new WebResult();
        webResult.setCode(ResultCode.FAILURE.getCode());
        webResult.setStatus(ResultStatus.failure);
        webResult.setDetail(resultCode.getMessage());
        return webResult;
    }

    public static WebResult setFailure(ResultCode resultCode, String token) {
        WebResult webResult = new WebResult();
        webResult.setStatus(ResultStatus.failure)
                .setCode(resultCode.getCode())
                .setDetail(resultCode.getMessage());
        return webResult;
    }

    public static WebResult setFailure() {
        WebResult webResult = new WebResult();
        webResult.setStatus(ResultStatus.failure)
                .setCode(ResultCode.FAILURE.getCode())
                .setDetail(ResultCode.FAILURE.getMessage());
        return webResult;
    }

    public static WebResult setFailure(String detail) {
        WebResult webResult = new WebResult();
        webResult.setStatus(ResultStatus.failure)
                .setCode(ResultCode.FAILURE.getCode())
                .setDetail(detail);
        return webResult;
    }

    public static WebResult setFailure(String detail, String token) {
        WebResult webResult = new WebResult();
        webResult.setToken(token)
                .setStatus(ResultStatus.failure)
                .setCode(ResultCode.FAILURE.getCode())
                .setDetail(detail);
        return webResult;
    }

    public static WebResult setFailure(Integer code, String detail, String token) {
        WebResult webResult = new WebResult();
        webResult.setToken(token)
                .setStatus(ResultStatus.failure)
                .setCode(code.toString())
                .setDetail(detail);
        return webResult;
    }

    public static WebResult setSuccess(String msg) {
        WebResult webResult = new WebResult();
        webResult.setDetail(msg);
        return webResult;
    }

    public static WebResult setSuccess(String msg, String token) {
        WebResult webResult = new WebResult();
        webResult.setToken(token).setDetail(msg);
        return webResult;
    }

    public static WebResult convert(PageResult result) {
        WebResult webResult = new WebResult(result.getPage(), result.getPageSize(), result.getTotalRow(), result.getTotalPage(), result.getInfo());
        webResult.setStatus(result.getStatus())
                .setCode(result.code())
                .setDetail(result.getDetail());
        return webResult;
    }

    public static WebResult setSuccessResult(Object obj) {
        WebResult webResult = new WebResult();
        webResult.setInfo(obj);
        return webResult;
    }

    public static WebResult setSuccessResult() {
        return new WebResult();
    }
}